//
//  DAContractRWDListViewController.m
//  ERP
//
//  Created by goldeney on 15/4/9.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "MTCContractRWDListViewController.h"

#import "MJRefresh.h"
#import "UITableView+Custom.h"
#import "NSDictionary+DataFormat.h"
#import "WebServiceCommonRequest.h"

#import "MTCContractShipmentViewCell.h"
#import "MTCContractRWDDetailViewController.h"

@interface MTCContractRWDListViewController ()

@property (strong, nonatomic) NSMutableArray *resultData;
@property (assign, nonatomic) NSInteger segmentActionState;

@property (assign, nonatomic) NSString *contractID;
@property (assign, nonatomic) NSString *projectID;
@property (assign, nonatomic) NSString *orderID;

@end

@implementation MTCContractRWDListViewController
@synthesize segmentedView = _segmentedView;
@synthesize tableView = _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithContractID:(NSString*)contractID
{
    self = [super init];
    if (self) {
        [self setTitle: @"合同任务单"];
        self.contractID = contractID;
    }
    return self;
}

- (id)initWithProjectID:(NSString*)projectID
{
    self = [super init];
    if (self) {
        [self setTitle: @"工程任务单"];
        self.projectID = projectID;
    }
    return self;
}

//- (id)initWithOrderID:(NSString*)orderID
//{
//    self = [super init];
//    if (self) {
//        [self setTitle: @"订单任务单"];
//        self.orderID = orderID;
//    }
//    return self;
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.resultData = [NSMutableArray array];
    self.tableView.tableFooterView= [[UIView alloc] init];
    
    [self.segmentedView setTintColor:ThemeColorBlue];
    [self.segmentedView addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self setupRefresh];
}

- (void)setupRefresh
{
    //添加下拉加载数据
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    [self.tableView headerBeginRefreshing];
    [self.tableView footerEndRefreshing];
}

- (void)stopRefresh
{
    //移除下拉加载数据
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

//具体委托方法实例
-(void)segmentAction:(UISegmentedControl *)Seg
{
    NSInteger index = Seg.selectedSegmentIndex;
    self.segmentActionState = index;
    
    [self.resultData removeAllObjects];
    [self.tableView reloadData];
    [self.tableView headerBeginRefreshing];
}


#pragma mark 网络获取数据

- (void)getNetWorkDataIsReresh:(BOOL)isReresh state:(NSNumber*)state end:(NSNumber*)end
{
    //任务单状态：0全部，1正供，2待供，3供毕
    NSString *RWDZTString = nil;
    if (self.segmentActionState == 1) {
        RWDZTString = [NSString stringWithFormat:@"2"];
    } else if (self.segmentActionState == 2) {
        RWDZTString = [NSString stringWithFormat:@"1"];
    } else if (self.segmentActionState == 3) {
        RWDZTString = [NSString stringWithFormat:@"3"];
    } else {
        RWDZTString = @"";
    }
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"HTGL_RWD_LIST"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"起始条数" value:state];
    [content addParameterKey:@"结束条数" value:end];
    [content addParameterKey:@"合同ID" value:self.contractID defaults:@"0"];
    [content addParameterKey:@"工程ID" value:self.projectID defaults:@"0"];
    [content addParameterKey:@"状态" value:RWDZTString];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(isReresh) [self.tableView headerEndRefreshing];
        else [self.tableView footerEndRefreshing];
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
            
        if(firstArray && firstArray.count>0){
            if(isReresh) [self.resultData removeAllObjects];
            [self.resultData addObjectsFromArray:[result.resultArray firstObject]];
            [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            [self.tableView reloadData];
        } else if(isReresh){
            [self.tableView setTableViewNotDataDrawText:@"暂无任务单" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"无法连接网络"];
    }];
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:0];
    NSNumber *endNumber = [NSNumber numberWithInteger:10];
    [self getNetWorkDataIsReresh:YES state:stateNumber end:endNumber];
}

- (void)footerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:self.resultData.count + 1];
    NSNumber *endNumber = [NSNumber numberWithInteger:self.resultData.count + 10];
    [self getNetWorkDataIsReresh:NO state:stateNumber end:endNumber];
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifierOne = @"MTCContractShipmentViewCellQDDJ";
    MTCContractShipmentViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierOne];
    if (!cell)
        cell = [[MTCContractShipmentViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierOne];
    
    //任务单信息设置
    NSDictionary *itemData = [self.resultData objectAtIndex:indexPath.row];
    cell.rwdbhLabel.text = [itemData objectFormatStringForKey:@"任务单编号" defaults:@"未知"];
    cell.gcmcLabel.text = [itemData objectFormatStringForKey:@"工程名称" defaults:@"未知"];
    cell.sgbwLabel.text = [itemData objectFormatStringForKey:@"施工部位" defaults:@"未知"];
    cell.jhkpLabel.text = [itemData objectFormatStringForKey:@"工地计划开盘时间" defaults:@"未知"];
    cell.ztLabel.text = [itemData objectFormatStringForKey:@"状态" defaults:@"未知"];
    cell.jhflLabel.text = [itemData objectFormatStringForKey:@"计划方量" format:@"计划%@方" defaults:@"未知"];
    cell.qddjLabel.text = [itemData objectFormatStringForKey:@"砼强度" defaults:@"未知等级"];
    cell.jzfsLabel.text = [itemData objectFormatStringForKey:@"浇筑方式" defaults:@"非泵送"];
    
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *itemData = [self.resultData objectAtIndex:indexPath.row];
    MTCContractRWDDetailViewController *viewController = [[MTCContractRWDDetailViewController alloc] initWithValue:itemData];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end